package com.molyfun.parents.activity.report.iepPlan;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.GetIepPlanTableResponse;
import com.molyfun.parents.rest.bean.IepTempListResponse;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.molyfun.parents.R.id.title_layout;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class IEPPlanCellListView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(title_layout)
    LinearLayout mTitleLayout;
    @InjectView(R.id.table_cell_title)
    TextView mTableTitle;
    @InjectView(R.id.table_cell_arrow)
    ImageView mTableCellArrow;

    @InjectView(R.id.container_layout)
    LinearLayout mContainerLayout;
    @InjectView(R.id.iep_plan_cell_layout)
    LinearLayout mIepPlanCellLayout;
    @InjectView(R.id.iep_plan_more_layout)
    RelativeLayout mIepPlanMoreLayout;

    private GetIepPlanTableResponse.IEPPlanTableBean.IeptemplistBean mIeptemplistBean;

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean isContainerOpen = false;

    public IEPPlanCellListView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public IEPPlanCellListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public IEPPlanCellListView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_iep_plan_cell_list_view, this);
        ButterKnife.inject(this);
        mIepPlanMoreLayout.setOnClickListener(this);
        mTitleLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iep_plan_more_layout:
                mCurrentPage ++;
                getMoreIEPPlanCellList();
                break;
            case title_layout:
                if(!isContainerOpen){
                    mContainerLayout.setVisibility(View.VISIBLE);
                    mTableCellArrow.setImageResource(R.mipmap.ic_table_up_srrow);
                    getMoreIEPPlanCellList();
                }else{
                    mCurrentPage = RestClient.DEFAULT_PAGE;
                    mTableCellArrow.setImageResource(R.mipmap.ic_table_down_srrow);
                    mIepPlanCellLayout.removeAllViews();
                    mContainerLayout.setVisibility(View.GONE);
                }
                isContainerOpen = !isContainerOpen;
                break;
        }
    }

    public void initData(GetIepPlanTableResponse.IEPPlanTableBean.IeptemplistBean ieptemplistBean) {
        initData(ieptemplistBean, false);
    }

    public void initData(GetIepPlanTableResponse.IEPPlanTableBean.IeptemplistBean ieptemplistBean, boolean autoLoad) {

        if (ieptemplistBean == null) {
            return;
        }

        mIeptemplistBean = ieptemplistBean;
        mTableTitle.setText(ieptemplistBean.getName());
        if(autoLoad){
            mTitleLayout.performClick();
        }
    }

    public void getMoreIEPPlanCellList() {
        Call<IepTempListResponse> call = MFPApplication.getInstance().getRestClient().getIepTempList(mIeptemplistBean.getId(), mCurrentPage);
        call.enqueue(new Callback<IepTempListResponse>() {
            @Override
            public void onResponse(Call<IepTempListResponse> call, Response<IepTempListResponse> response) {
                if (response.body() != null && response.body().getData() != null) {
                    if (response.body().getData().size() > 0) {
                        for(GetIepPlanTableResponse.IEPPlanTableBean.IeptemplistBean ieptemplistBean : response.body().getData()){
                            View singleCell = layoutInflater.inflate(R.layout.layout_iep_plan_single_cell, null, false);
                            if(ieptemplistBean.getIeptempimglist() != null && ieptemplistBean.getIeptempimglist().size() > 0){
                                singleCell.findViewById(R.id.attachment_viewer).setVisibility(View.VISIBLE);
                                ((AttachmentViewer)singleCell.findViewById(R.id.attachment_viewer)).initData(ieptemplistBean.getIeptempimglist());
                            }else{
                                (singleCell.findViewById(R.id.attachment_viewer)).setVisibility(View.GONE);
                            }
                            ((TextView)singleCell.findViewById(R.id.iep_plan_single_cell_title)).setText("内容: " + ieptemplistBean.getName());
                            ((TextView)singleCell.findViewById(R.id.iep_plan_single_cell_time)).setText("时间: " + ieptemplistBean.getTrainingdatetime() + "~" + ieptemplistBean.getTrainingdatetimeend());
                            mIepPlanCellLayout.addView(singleCell);
                        }
                        if(response.body().getData().size() < 2){
                            mIepPlanMoreLayout.setVisibility(View.GONE);
                        }else{
                            mIepPlanMoreLayout.setVisibility(View.VISIBLE);
                        }
                    } else {
                        mIepPlanMoreLayout.setVisibility(View.GONE);
                    }
                }else{
                    mIepPlanMoreLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<IepTempListResponse> call, Throwable t) {
                mIepPlanMoreLayout.setVisibility(View.GONE);
            }
        });
    }

}